rm(list = ls())
#replication file path
replication_file_path = '/path/to/this/replication/file'
setwd(replication_file_path)

#Paths to restricted data
ipums_census_path = "/path/to/restricted/data/from/ipums"
cwdb_path =  "/path/to/restricted/data/from/cwdb"
ancestry_path = "/path/to/restricted/data/from/ancestry"

#path to fasttext function
fasttext_path = "/path/to/fasttext/function/fasttext"

#Run code to create data used in analysis
make_data = F
#Use restricted data to create analysis data
use_restricted = F
#Run fasttext to generate predicted partisanship
run_fasttext = F
#Run fastlink to generate matches
run_fastlink = F

#load packages
packages = c('broom', 'caret', 'CBPS', 'cowplot', 'data.table', 'dotwhisker', 
             'eiPartialID', 'fastLink', 'foreign', 'ggplot2', 'ggpubr', 'grf', 
             'grid', 'gridExtra', 'igraph', 'interflex', 'kableExtra', 'knitr', 'lfe', 'lmtest', 
             'magrittr', 'Matrix', 'matrixStats', 'phonics', 'PRROC', 'rmarkdown', 
             'rmarkdown', 'RSQLite', 'sandwich', 'stargazer', 'stringdist', 'stringi', 'stringr', 'modelsummary')
invisible(lapply(packages, function(x) if (!require(x, character.only=T)){
  install.packages(x, repos = "http://cran.us.r-project.org");library(x, character.only = T)}))

#Load custom functions
source('./tools/clean_historical_names.R')
source('./tools/my_fastLink.R')
source("./tools/write_latex.R")
source("./tools/ei_w1_bounds.R")


#Check... is restricted data present?
if (use_restricted) {
  ipums_files = c("indiana_1860.txt", "iowa_1860.txt", "iowa_1870.txt", 'wisconsin_1860.txt', 'wisconsin_1870.txt')
  cwdb_files = c("assignus.csv", "person_table.csv", "regcas.csv", "regiment_roster_table.csv", "regiments.csv")
  ancestry_files = 'incw.db'
  
  use_restricted = check_restricted = c(paste(ipums_census_path, ipums_files, sep = "/"),
                      paste(cwdb_path, cwdb_files, sep = "/"),
                      paste(ancestry_path, ancestry_files, sep = "/")
                      ) %>% 
                  file.exists() %>% 
                  all()
  if (!check_restricted) {
    cat("Restricted data is missing. Setting `use_restricted` to F.")
  }
}

#check... is fasttext present?
if (run_fasttext) {
  run_fasttext = check_fasttext = file.exists(fasttext_path)
  if (!check_fasttext) {
    cat("fastText function cannot be located. Setting `run_fasttext` to F.")
  }
}

###########################
#Prepare Data for Analysis#
###########################

if (make_data) {
  #Make data for Main Analyses
  source('./data/county_elections/make_county_elections.R')
  
  #Make data for Indiana Soldiers
  source('./data/indiana_soldiers/make_indiana_soldiers.R')
  
  #Make data for Suffrage Referenda Analyses
  source('./data/suffrage_referenda/make_suffrage_referenda.R')
    
  #Make data for Iowa Legislators
  source('./data/iowa_legislators/make_iowa_legislators.R')
}

####################
#Replicate Analyses#
####################

table_list = list()

#County Enlistment and Republican Voteshare
source("./analysis/analysis_gop_vs.R")

#Indiana Soldiers analysis
source("./analysis/analysis_indiana_soldiers.R")

#Wisconsin/Iowa Suffrage analysis
source("./analysis/analysis_suffrage.R")

#Suffrage Analysis: Ecological Regression simulation
rmarkdown::render(input = "./analysis/analysis_ei_sim.Rmd", 
                  output_format = "pdf_document",
                  output_file = 'analysis_ei_sim.pdf',
                  output_dir = "./output/appendix", quiet = T)

#Suffrage Analysis: Ecological Inference
source("./analysis/analysis_suffrage_ei.R")

#Iowa Legislator Analysis:
source("./analysis/analysis_legislators.R")

#Wisconsin Newspapers Data
source("./analysis/analysis_newspapers.R")

#Full Tables
table_table = rbindlist(table_list)
table_table[, sub := str_extract(name, "(?<=\\s)\\w")]
table_table[, item := str_extract(name, "\\d+$") %>% as.numeric]

setkey(table_table, sub, item, name)

rmarkdown::render(input = "./analysis/analysis_tables.Rmd", 
                  output_format = "html_document",
                  output_file = 'Supplementary_Tables.html',
                  output_dir = "./output", quiet = T)